// var rtcroom = require('./utils/rtcroom.js');
// var liveroom = require('./utils/liveroom.js');
// var config = require('./config.js');
var util = require('./utils/util.js')

//app.js
App({
  onLaunch: function (options) {
    var self=this;
    // 获取转发者的appid
    if(options.referrerInfo){
      this.globalData.shareAppId = options.referrerInfo.appId
    }
    if (options.shareTickets) {
      // 获取转发详细信息
      wx.getShareInfo({
        shareTicket: options.shareTickets[0],
        success(res) {
          res.errMsg; // 错误信息
          res.encryptedData;  //  解密后为一个 JSON 结构（openGId    群对当前小程序的唯一 ID）
          res.iv; // 加密算法的初始向量
        },
        fail() {},
        complete() {}
      });
    }
    wx.getSystemInfo({
        success(res){
          console.log(res)
          self.systemInfo=res;
        }
      })
    // console.log("***shareTicket***",options.shareTickets);
    //调用API从本地缓存中获取数据
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)
      // this.login(function(obj){
      //     var param={
      //         "nickname":self.globalData.userInfo?self.globalData.userInfo.nickName:"",
      //         "mobile":"",
      //         "source":"3",
      //         "customerHead":self.globalData.userInfo?self.globalData.userInfo.avatarUrl:"",
      //         "module":options.loginid||"小程序",
      //         "houseid":self.globalData.houseid,
      //         "appVersion":"1.0",
      //         "openId":self.globalData.openid,
      //         "code":obj.code||"",
      //         "appId":obj.appid||"",
      //         "secret":obj.secret||"",
      //     }
      //     console.log(param,'param')
      //     wx.request({
      //         url: util.url(),
      //         method: 'POST',
      //         data: util.reformParam(util.mineLogin, param),
      //         header: {
      //             'content-type': 'application/json' // 默认值
      //         },
      //         success:function(res){
      //             console.log("***app.login***",res);
      //             if(res.data.success){
      //                 if(res.data.single){ // 获取信息成功
      //                     var data = res.data.single;
      //                     self.globalData.qrpictureurl = data.qrpictureurl;
      //                     self.globalData.tonken = data.tonken;
      //                     self.globalData.single = data;
      //                     self.globalData.loginid = data.loginid;
      //                     self.globalData.sessionKey=data.sessionKey;
      //                     self.globalData.openid = data.openId||"";
      //                     self.globalData.phone = wx.getStorageSync('phone');
      //                     self.globalData.identifier = data.id+"_"+data.loginid+"_"+data.houseid;

      //                     self.proto_getLoginInfo(self);
      //                 }
      //                 else{
      //                     wx.showToast({
      //                         title: "数据不存在,请稍后再试",
      //                         icon: 'warn',
      //                         duration: 1500,
      //                     })
      //                 }
      //             }
      //             else{
      //                 wx.showToast({
      //                     title: res.data.message,
      //                     icon: 'warn',
      //                     duration: 1500,
      //                 })
      //             }
      //         },
      //         fail: function (ret) {
      //             console.log('获取信息失败: ', ret);
      //             if (ret.errMsg == 'request:fail timeout') {
      //                 wx.showToast({
      //                     title: "网络请求超时，请检查网络状态",
      //                     icon: 'warn',
      //                     duration: 1500,
      //                 })
      //             }
      //             else{
      //                 wx.showToast({
      //                     title: ret.errMsg,
      //                     icon: 'warn',
      //                     duration: 1500,
      //                 })
      //             }
      //         }
      //     });
      // })
  },
  proto_getLoginInfo: function (app) {
    wx.request({
      url: util.url(),
      method: 'POST',
      data: util.reformParam(util.tencentSignature, { identifier: this.globalData.identifier, appId: '1400091043' }),
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (ret) {
        if (ret.data.code) {
          console.log('获取登录信息失败，调试期间请点击右上角三个点按钮，选择打开调试');
          wx.showToast({
            title: ret.data.message + '[' + ret.data.code + ']',
            icon: 'warn',
            duration: 1500,
          })
          return;
        }
        console.log('获取usersig登录信息成功',app,ret.data.single.signature);
        app.globalData.userSig = ret.data.single.signature;
      },
      fail: function (ret) {
          console.log('获取IM信息失败: ', ret);
          if (ret.errMsg == 'request:fail timeout') {
            wx.showToast({
              title: "网络请求超时，请检查网络状态",
              icon: 'warn',
              duration: 1500,
            })
          }
          else{
            wx.showToast({
              title: ret.errMsg,
              icon: 'warn',
              duration: 1500,
            })
          }
      }
    });
  },
  getToken: function (cb) {
    var that = this;
    // console.log("getToken:",this.globalData)
    if(this.globalData.tonken){
      typeof cb == "function" && cb(this.globalData.tonken)
    } else {
      var data = util.reformParam(util.tokenUrl,{
          "nickname":"",
          "mobile":"",
          "verifycode":"",
          "invitation_code":"",
          "password":"",
          "source":"3",
          "new_guested":"1",
          "module":"",
          "houseid":"109",
          "anonymous":"1",
          "appVersion":"150"
      });
      wx.request({
        url: util.url(),
        // url: "http://139.196.5.59:9005/skyforest/dynatown/list",
        method:'POST',
        data: data,
        success:function(res){
          console.log(res,'app.js *** getToken')
          that.globalData.tonken = res.data.single.tonken;
          typeof cb == "function" && cb(that.globalData.tonken)
        }
      })
    }
  },
  getUserInfo:function(cb){
    var that = this;
    //取出本地存储用户信息，解决需要每次进入小程序弹框获取用户信息
    that.globalData.userInfo = wx.getStorageSync('userInfo');
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      // wx.login({
      //   success: function (res) {
      //     debugger
      //     if(res.code){
      //       var secret = that.globalData.secret;//"4c75d6c6ace1e8d6aa747113ee2125c8";
      //       var code = res.code;
      //       that.globalData.code =res.code;
      //       var appid = that.globalData.appid;//"wxa6aef323462b8b14";
      //       wx.request({
      //         url:'https://api.weixin.qq.com/sns/jscode2session?appid='+appid+'&secret='+secret+'&js_code='+code+'&grant_type=authorization_code',
      //         data:{code:res.code},
      //         success:function(res){
      //           console.log(res)
      //          that.globalData.openid = res.data.openid;
      //          that.globalData.sessionKey = res.data.session_key;
      //         }
      //       })
      //     }
      //   }
      // })
    }
  },
  login:function(cb){
      var that = this
      //取出本地存储用户信息，解决需要每次进入小程序弹框获取用户信息
      that.globalData.userInfo = wx.getStorageSync('userInfo');
      //调用登录接口
      wx.login({
        success: function (res) {
          if(res.code){
            var secret = that.globalData.secret;//"4c75d6c6ace1e8d6aa747113ee2125c8";
            var code = res.code;
            that.globalData.code =res.code;
            var appid = that.globalData.appid;//"wxa6aef323462b8b14";
            var obj={
              code:code,
              appid:appid,
              secret:secret,
            }
            console.log("=========login-code======",obj)
            typeof cb == "function" && cb(obj)
          }
        }
      })
  },
  systemInfo:{},
  globalData:{
    appid:"wx214ab345ac6f94f2",
    secret:"de8fdfd8fd2294c1b44d0325c9a38d52",
    openid:null,
    sessionKey:null,
    shareAppId:null,
    tonken:null,
    houseid:"109",
    code:null,
    fromChannel:null,
    qrpictureurl:null,
    identifier:null,
    single:null,
    tmpPhone:false,
    globalUserInfoFlag:false,
    phone:null,
    userSig:null,
    like:null,
    view:null,
    parm:null,
    loginid:null,
    userInfo:null,
    templateUrl:'https://h5test.elab-plus.com/launch/#/pages/preview?projectId=', // 模板H5链接前缀
  }
})